
package com.cennavi.minenavidemo.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cennavi.minenavidemo.R;

/**
 * 多选按钮控件
 */
public class MultipleChoiceButton extends LinearLayout {

	public interface OnChoiceChangeListener {
		void onChoiceChange(int position);
	}

	private Context mContext;
	private TextView[] mContentViews;
	private OnChoiceChangeListener mOnChoiceChangeListener;
	private int mSingleContentMaxWidth;

	public MultipleChoiceButton(Context context) {
		super(context);
		init(context);
	}

	public MultipleChoiceButton(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MultipleChoiceButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}


	public void init(Context context) {
		mContext = context;
	}

	public void measureSingleContentMaxWidth(String[] contents) {
		String textMaxWidthContent = "";
		for (int i = 0; i < contents.length; i++) {
			if (textMaxWidthContent.length() < contents[i].length()) {
				textMaxWidthContent = contents[i];
			}
		}
		TextView textView = new TextView(mContext);
		textView.setGravity(Gravity.CENTER);
		textView.setText(textMaxWidthContent);
		int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		textView.measure(spec, spec);
		mSingleContentMaxWidth = textView.getMeasuredWidth() + getResources().getDimensionPixelSize(R.dimen.nz_px_40);
	}

	public void setContent(String[] contents) {
		int contentViewCount = contents.length;
		measureSingleContentMaxWidth(contents);
		mContentViews = new TextView[contentViewCount];
		int singleContentViewWidth;
		if (getWidth() == 0) {
			singleContentViewWidth = mSingleContentMaxWidth;
		} else {
			singleContentViewWidth = getWidth() / contentViewCount;
		}
		removeAllViews();
		for (int i = 0; i < contentViewCount; i++) {
			TextView textView = new TextView(mContext);
			textView.setGravity(Gravity.CENTER);
			textView.setText(contents[i]);
			final int finalI = i;
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					setChoice(finalI, true);
				}
			});
			LayoutParams layoutParams = new LayoutParams(singleContentViewWidth, ViewGroup.LayoutParams.MATCH_PARENT);
			mContentViews[i] = textView;
			addView(textView, layoutParams);
		}
	}

	public void setChoice(int position) {
		setChoice(position, false);
	}

	private void setChoice(int position, boolean isCallback) {
		int contentViewCount = mContentViews.length;
		for (int i = 0; i < contentViewCount; i++) {
			TextView textView = mContentViews[i];
			Drawable contentViewBg = null;
			if (i == 0) {
				if (i == position) {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_left_choice_bg);
				} else {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_left_unchoice_bg);
				}
			} else if (i == contentViewCount - 1) {
				if (i == position) {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_right_choice_bg);
				} else {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_right_unchoice_bg);
				}
			} else {
				if (i == position) {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_middle_choice_bg);
				} else {
					contentViewBg = getResources().getDrawable(R.drawable.multiple_choice_button_middle_unchoice_bg);
				}
			}
			textView.setBackground(contentViewBg);
			if (i == position) {
				textView.setTextColor(getResources().getColor(R.color.MultipleChoiceButtonChoiceText));
			} else {
				textView.setTextColor(getResources().getColor(R.color.MultipleChoiceButtonUnchoiceText));
			}
		}
		if (mOnChoiceChangeListener != null && isCallback) {
			mOnChoiceChangeListener.onChoiceChange(position);
		}
	}

	public void setOnChoiceListener(OnChoiceChangeListener onChoiceChangeListener) {
		mOnChoiceChangeListener = onChoiceChangeListener;
	}

}
